package cn.com.guage.dtm;

import java.util.Arrays;

// 定义一个泛型类 SequenceList
public class SequenceList<T> {
    private static final int DEFAULT_CAPACITY = 10;
    private T [] elements;
    private int size;

    // 构造函数，初始化顺序表
    public SequenceList() {
        this.elements = (T[]) new Object[DEFAULT_CAPACITY];
        this.size = 0;
    }

    // 获取顺序表的大小
    public int size() {
        return size;
    }

    // 判断顺序表是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 在顺序表末尾添加元素
    public void add(T element) {
        ensureCapacity(size + 1);
        elements[size++] = element;
    }

    // 在指定位置插入元素
    public void add(int index, T element) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        ensureCapacity(size + 1);
        System.arraycopy(elements, index, elements, index + 1, size - index);
        elements[index] = element;
        size++;
    }

    // 获取指定位置的元素
    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        return elements[index];
    }

    // 修改指定位置的元素
    public T set(int index, T element) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        T oldValue = elements[index];
        elements[index] = element;
        return oldValue;
    }

    // 删除指定位置的元素
    public T remove(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        T oldValue = elements[index];
        int numMoved = size - index - 1;
        if (numMoved > 0) {
            System.arraycopy(elements, index + 1, elements, index, numMoved);
        }
        elements[--size] = null;
        return oldValue;
    }

    // 确保顺序表有足够的容量
    private void ensureCapacity(int minCapacity) {
        if (minCapacity > elements.length) {
            int newCapacity = elements.length * 2;
            if (newCapacity < minCapacity) {
                newCapacity = minCapacity;
            }
            elements = Arrays.copyOf(elements, newCapacity);
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(elements[i]);
            if (i < size - 1) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static void main(String[] args) {
        SequenceList<Integer> list = new SequenceList<>();
        // 添加元素
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println("添加元素后: " + list);

        // 在指定位置插入元素
        list.add(1, 4);
        System.out.println("在位置 1 插入元素后: " + list);

        // 获取指定位置的元素
        System.out.println("位置 2 的元素是: " + list.get(2));

        // 修改指定位置的元素
        list.set(0, 5);
        System.out.println("修改位置 0 的元素后: " + list);

        // 删除指定位置的元素
        list.remove(1);
        System.out.println("删除位置 1 的元素后: " + list);
    }
}